<?php
namespace app\appclass\model;
use app\BaseModel;
use app\card\model\Company;
use app\card\model\User;
use app\publics\model\TmplConfig;
use app\radar\model\RadarCount;
use app\shop\model\IndexUser;
use longbingcore\wxcore\WxTmpl;
use think\facade\Db;



class ClassRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_class_record';




    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis,$file='*'){

        $data = $this->where($dis)->field($file)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            $cate_model      = new ClassCate();

            $company_model   = new Company();

            $room_model      = new ClassRoom();

            $card_user_model = new CardUser();

            foreach ($data['data'] as &$v){
                //门店
                $v['store_name'] = $company_model->where(['id'=>$v['store_id']])->value('name');
                //教室
                $v['room_name']  = $room_model->where(['id'=>$v['room_id']])->value('title');

                $v['coach_name'] = lbUserName($v['coach_id']);

                $card_info = $card_user_model->dataInfo(['id'=>$v['card_id']]);

                $v['card_name']   = !empty($card_info)?$card_info['card_name']:'';

                $v['card_code']   = $card_info['code'];

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                $v['start_time']  = date('Y-m-d H:i:s',$v['start_time']);

                $v['cate_name']   = $cate_model->where(['id'=>$v['cate_id']])->value('title');

                //取消人

                if($v['cancel_user']==-1){

                    $v['cancel_name'] = '课程预约未达到人数，系统自动取消';

                }elseif ($v['cancel_user']>0){
                    //员工取消
                    $v['cancel_name'] = lbUserName($v['cancel_user']);

                }else{
                    //自己取消
                    $v['cancel_name'] = '';
                }

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 11:12
     * @功能说明:检查私交课的时间
     */
    public function checkPrivateTime($class_id,$start_time,$coach_id){

        $class_model = new ClassList();

        $store_model = new StoreInfo();

        $class_info = $class_model->dataInfo(['id'=>$class_id]);
        //结束时间
        $end_time   = $start_time+$class_info['time_long']*60;

        $store_id   = $class_model->getStoreId($coach_id);
        //门店信息
        $store_info = $store_model->dataInfo(['store_id'=>$store_id]);

        if(empty($store_info['end_time'])||empty($store_info['start_time'])){

            $this->errorMsg('教师不在服务时间内1');
        }

        $store_end_time   = strtotime($store_info['end_time'])-strtotime("today")+strtotime(date('Y-m-d',$start_time));

        $store_start_time = strtotime($store_info['start_time'])-strtotime("today")+strtotime(date('Y-m-d',$start_time));

        if($store_start_time>$start_time||$store_end_time<$end_time){

            $this->errorMsg('教师不在服务时间内2');

        }
        //检查排课情况
        $res = $this->checkCoachTime($start_time,$end_time,$coach_id);

        if($res==false){

            $this->errorMsg('教师不在服务时间内3');
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 13:48
     * @功能说明:检查教师排课时间和私教时间
     *
     */
    public function checkCoachTime($start_time,$end_time,$coach_id,$i=0,$coach_class=[],$class_data=[]){

        $date_model  = new ClassDate();

        $record_model= new ClassRecord();

        if($i==0){
            //查询该教师所有未开始的私教课
            $coach_class = $record_model->where(['status'=>1,'coach_id'=>$coach_id,'class_type'=>1])->select()->toArray();
            //查询排课
            $where[] = ['coach_id','=',$coach_id];

            $where[] = ['start_time','>',time()];

            $where[] = ['status','=',1];

            $class_data  = $date_model->where($where)->select()->toArray();
        }


        if($start_time<time()){

            return false;
        }

        //检查私教课
        if(!empty($coach_class)){

            foreach ($coach_class as $vs){

                $check = is_time_cross($start_time,$end_time,$vs['start_time'],$vs['end_time']);

                if($check==false){

                    return $check;

                }

            }

        }
        //检查排课
        if(!empty($class_data)){

            foreach ($class_data as $vs){

                $check = is_time_cross($start_time,$end_time,$vs['start_time'],$vs['end_time']);

                if($check==false){

                    return false;

                }

            }

        }

        return true;

    }

    /**
     * @param $coach_id
     * @功能说明:g根据教师获取时间
     * @author chenniang
     * @DataTime: 2021-05-18 17:37
     */
    public function getCoachTime($coach_id,$time=0){

        $class_model = new ClassList();

        $store_model = new StoreInfo();

        $date_model  = new ClassDate();

        $record_model= new ClassRecord();

        $store_id = $class_model->getStoreId($coach_id);
        //门店信息
        $store_info = $store_model->dataInfo(['store_id'=>$store_id]);

        $time_list  = [];
        //时间段
        if(!empty($store_info['end_time'])&&!empty($store_info['start_time'])){

            $time = strtotime($time);

            $end_time   = strtotime($store_info['end_time']) -strtotime("today")+$time;

            $start_time = strtotime($store_info['start_time'])-strtotime("today")+$time;

            $i = 0;

            while ($start_time<$end_time){

                $start_time  = $start_time+15*60;

                $time_list[$i]['time_str']  = $start_time;

                $time_list[$i]['time_text'] = date('H:i',$start_time);

                $i++;

            }

        }

        if(!empty($time_list)){
            //查询该教师所有未开始的私教课
            $coach_class = $record_model->where('status','in',[1,3])->where(['coach_id'=>$coach_id,'class_type'=>1])->select()->toArray();
            //查询排课
            $where[] = ['coach_id','=',$coach_id];

            $where[] = ['start_time','>',time()];

            $where[] = ['status','=',1];

            $class_data  = $date_model->where($where)->select()->toArray();

            foreach ($time_list as $k=>&$value){

                $time_s = $value['time_str'];

                $time_e = $value['time_str']+60*15-1;

                $res = $this->checkCoachTime($time_s,$time_e,$coach_id,1,$coach_class,$class_data);

                $value['status'] = $res==true?1:0;

            }

        }

        return $time_list;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 14:26
     * @功能说明:课程数量
     */
    public function classCount($dis,$where=[]){
        //公课
//        $dis['class_type'] = 0;
//
//        $public = $this->where($dis)->where($where)->where('status','<>',2)->group('class_date_id')->count();

        $date_model = new ClassDate();

        $public = $date_model->where($dis)->where($where)->where('status','in',[1,2])->count();
        //私课
        $dis['class_type'] = 1;

        $private = $this->where($dis)->where($where)->where('status','<>',2)->count();

        return $public+$private;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 18:57
     * @功能说明:会员卡使用次数
     */
    public function cardUseCount($card_id,$type=1){

        if($type==1){

            $num = $this->where(['card_id'=>$card_id])->where('status','<>',2)->whereTime('create_time','toDay')->sum('num');

        }elseif ($type==2){

            $num = $this->where(['card_id'=>$card_id])->where('status','<>',2)->whereTime('create_time','week')->sum('num');

        }elseif ($type==3){

            $num = $this->where(['card_id'=>$card_id])->where('status','<>',2)->whereTime('create_time','month')->sum('num');

        }

        return $num;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-31 14:21
     * @功能说明:会员预约次数排序
     */
    public function userRecordTop($dis,$where,$page=10){

        $user_model = new IndexUser();

        $data = $user_model->alias('a')
                ->join('longbing_card_appclass_class_record b','a.id = b.user_id')
                ->join('longbing_card_user_phone c','a.id = c.user_id','left')
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field('a.nickName,a.avatarUrl,SUM(b.num) as record_num,b.user_id,c.phone')
                ->group('a.id')
                ->order('record_num desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 10:10
     * @功能说明:员工上过哪些课
     */
    public function coachSuccessClass($coach_id){

        $dis = [

            'coach_id'  => $coach_id,

            'class_type'=> 1,

        ];
        //私教课
        $private = $this->where($dis)->where('status','in',[3])->group('class_id')->column('class_id');

        $date_model = new ClassDate();

        $dis = [

            'coach_id' => $coach_id,

           // 'status'   => 2
        ];
        //公课
        $public = $date_model->where($dis)->where('status','in',[2])->column('class_id');

        return array_merge($public,$private);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 11:37
     * @功能说明:获取课程售价
     */
    public function classSalePrice($class_id,$coach_id='',$start_time='',$end_time='',$type=1){

        $dis = [

           // 'class_id' => $class_id,

            'card_type'=> 2

        ];

        if($type==1){

            $dis['class_id'] = $class_id;

        }else{

            $dis['class_date_id'] = $class_id;

        }

        if(!empty($coach_id)){

            $dis['coach_id'] = $coach_id;
        }

        $where[] = ['status','in',[1,3]];

        if(!empty($start_time)&&!empty($end_time)){

            $where[] = ['start_time','between',"$start_time,$end_time"];

        }



        $data = $this->where($dis)->where($where)->sum('true_value');

        return round($data,2);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 13:41
     * @功能说明:获取教师上课节数
     */

    public function classCoachCount($cate_type,$coach_id,$cate_id='',$class_id = '',$start_time='',$end_time=''){

        $where = [

            'coach_id'    => $coach_id,

        ];

        if(!empty($cate_id)){

            $where['cate_id'] = $cate_id;
        }

        if(!empty($class_id)){

            $where['class_id'] = $class_id;
        }

        $dis = [];

        if(!empty($start_time)&&!empty($end_time)){

            $dis[] = ['start_time','between',"$start_time,$end_time"];

        }
        //私教课
        if($cate_type==1){

            $where['status']     = 3;

            $where['class_type'] = 1;

            $class_count = $this->where($where)->where($dis)->count();

        }else{

            $where['status']  = 2;

            $date_model  = new ClassDate();
            //团课
            $class_count = $date_model->where($where)->where($dis)->count();
        }

        return $class_count;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 15:06
     * @功能说明:员工端
     *
     */
    public function staffIndexList($dis){

        //预约人
        $data = $this->where($dis)->order('start_time,id')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $user_model = new IndexUser();

            $card_model = new CardUser();

            $water_model= new CardWater();

            foreach ($data['data'] as &$v){
                //用户昵称
                $v['nickName']  = $user_model->where(['id'=>$v['user_id']])->value('nickName');
                //用户头像
                $v['avatarUrl'] = $user_model->where(['id'=>$v['user_id']])->value('avatarUrl');
                //
                $v['day_text']  = date('m-d',$v['start_time']);

                $v['year_text'] = date('Y',$v['start_time']);

                $v['time_text'] = date('H:i',$v['start_time']);

                $card_info = $card_model->dataInfo(['id'=>$v['card_id']]);
                //会员卡名字
                $v['card_title'] = $card_info['card_name'];
                //当前余额
                if(in_array($v['card_type'],[1,2])){

                    $v['current_card_value'] = $water_model->where(['record_id'=>$v['id']])->value('n_value');

                }

            }

        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 15:28
     * @功能说明:课程取消
     */
    public function recordCancel($id,$cancel_user = 0,$msg=1,$time=0){

        $data = $this->dataInfo(['id'=>$id]);

        if($data['status']!=1&&$msg==1){

            $this->errorMsg('状态错误');
        }

        if($data['start_time']<time()&&$msg==1){

            $this->errorMsg('课程已开始');

        }
        //取消时间 有可能是系统自动取消
        $time = !empty($time)?$time:time();

        Db::startTrans();

        $res = $this->dataUpdate(['id'=>$id],['status'=>2,'cancel_user'=>$cancel_user,'cancel_time'=>$time]);

        if($res==0){

            Db::rollback();

            if($msg==1){

                $this->errorMsg('取消失败');
            }

        }

        $card_model = new CardUser();

        $card_info  = $card_model->dataInfo(['id'=>$data['card_id']]);
        //增加退款流水
        $water_model = new CardWater();

        $res = $water_model->recordWaterAdd($id,$data['true_value'],$data['card_id'],1);

        if($res==0){

            Db::rollback();

            if($msg==1){

                $this->errorMsg('取消失败');
            }
        }
        //减会员卡对余额或者次数
        if(in_array($data['card_type'],[1,2])){

            $res = $card_model->dataUpdate(['id'=>$data['card_id'],'i'=>$card_info['i']],['true_value'=>$card_info['true_value']+$data['true_value'],'i'=>$card_info['i']+1]);

            if($res==0){

                Db::rollback();

                if($msg==1){

                    $this->errorMsg('取消失败');
                }
            }

        }

        $date_model = new ClassDate();
        //退换排课的已经预约人数 如果是系统自动取消就不退还人数
        if(!empty($data['class_date_id'])&&$msg=1){

            $date_info = $date_model->dataInfo(['id'=>$data['class_date_id']]);

            $res = $date_model->dataUpdate(['id'=>$date_info['id'],'i'=>$date_info['i']],['have_num'=>$date_info['have_num']-$data['num'],'i'=>$date_info['i']+1]);

            if($res==0){

                Db::rollback();

                if($msg==1){

                    $this->errorMsg('取消失败');

                }

            }

        }

        //发送雷达消息
        $modelCount = new RadarCount();
        //员工暂不通知
        $arr = [$data['coach_id']];

        foreach ($arr as $value){

            if($value){

                $insert = array(

                    'user_id' => $data['user_id'],

                    'to_uid'  => $value,

                    'type'    => 672
                ,

                    'sign'    => 'view',

                    'scene'   => 0,

                    'uniacid' => $data['uniacid'],

                    'target'  => $data['id'],

                    'start_time' => $data['start_time'],

                    'create_time' => time()
                );
                //时长
                $result = $modelCount->insert( $insert );

                if($result==0){

                    Db::rollback();

                    $this->errorMsg('取消失败');
                }

                $insert_id = $modelCount->getLastInsID();

                longbingSendWxServiceNotice($insert_id);

            }

        }

        Db::commit();

        $this->cancelOrderTmp($data);

        return true;
    }




    /**
     * @author chenniang
     * @DataTime: 2021-05-17 16:41
     * @功能说明:预约
     */
    public function appClassDate($input,$uniacid,$user_id,$add_user=0){

        $dis = [

            'id'     => $input['id'],

            'status' => 1
        ];

        $date_model = new ClassDate();

        $data = $date_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('课程已下架或课程已结束');

        }

        if($data['end_time']<time()){

            $this->errorMsg('课程已经结束');
        }

        if($data['have_num']+$input['num']>$data['num']){

            $this->errorMsg('超出课程人数');

        }

        $card_model   = new CardUser();

        $record_model = new ClassRecord();

        $class_card_model = new ClassCard();
        //检查会员卡
        $check_card = $class_card_model->checkCard($data,$input['num'],$input['card_id'],$user_id);

        $insert = [

            'uniacid'    => $uniacid,

            'user_id'    => $user_id,

            'class_id'   => $data['class_id'],

            'store_id'   => $data['store_id'],

            'coach_id'   => $data['coach_id'],

            'class_date_id'=> $data['id'],

            'cate_id'    => $data['cate_id'],

            'room_id'    => $data['room_id'],

            'time_long'  => $data['time_long'],

            'start_time' => $data['start_time'],

            'end_time'   => $data['end_time'],

            'title'      => $data['title'],

            'difficulty' => $data['difficulty'],

            'card_id'    => $input['card_id'],

            'card_type'  => $check_card['card_type'],

            'num'        => $input['num'],

            'record_text'=> $input['record_text'],

            'value'      => $check_card['value'],

            'true_value' => $check_card['true_value'],
            //课程类型
            'class_type' => 0,

            'status'     => 1,

            'add_user'   => $add_user,

            'staff_id'   => !empty($input['staff_id'])?$input['staff_id']:0
         ];

        Db::startTrans();

        $res = $record_model->dataAdd($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }
        //添加流水记录
        $water_model = new CardWater();

        $record_id = $record_model->getLastInsID();

        $res = $water_model->recordWaterAdd($record_id,$insert['true_value'],$insert['card_id']);

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }
        //减会员卡对余额或者次数
        if(in_array($check_card['card_type'],[1,2])){

            $res = $card_model->dataUpdate(['id'=>$input['card_id']],['true_value'=>$check_card['card_true_value']-$insert['true_value']]);
        }

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }

        $res = $date_model->dataUpdate(['id'=>$input['id'],'i'=>$data['i']],['have_num'=>$data['have_num']+$input['num'],'i'=>$data['i']+1]);

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }
        //发送雷达消息
        $modelCount = new RadarCount();
        //员工暂不通知
        $arr = [$data['coach_id']];

        $arr = array_unique($arr);

        foreach ($arr as $value){

            if($value){

                $data = array(

                    'user_id' => $user_id,

                    'to_uid'  => $value,

                    'type'    => 671,

                    'sign'    => 'view',

                    'scene'   => 0,

                    'uniacid' => $uniacid,

                    'target'  => $record_id,

                    'start_time' => $data['start_time'],

                    'create_time' => time()
                );
                //时长
                $result = $modelCount->insert( $data );

                if($result==0){

                    Db::rollback();

                    $this->errorMsg('预约失败');
                }

                $insert_id = $modelCount->getLastInsID();

                longbingSendWxServiceNotice($insert_id);

            }

        }

        Db::commit();
        //订阅消息预约成功通知
        $this->successOrderTmp($insert);

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:39
     * @功能说明:预约私交课
     */
    public function appPrivateClass($input,$uniacid,$user_id,$add_user = 0){

       $class_model = new ClassList();

        $dis = [

            'id'     => $input['class_id'],

            'status' => 1
        ];

        $data = $class_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('课程已下架');
        }

        $card_model   = new CardUser();

        $record_model = new ClassRecord();

        $class_card_model = new ClassCard();

        $input['end_time'] = $input['start_time']+$data['time_long']*60;
        //检查教师时间
        $this->checkPrivateTime($input['class_id'],$input['start_time'],$input['coach_id']);
        //检查会员卡
        $check_card = $class_card_model->checkCard($input,$input['num'],$input['card_id'],$user_id);

        $insert = [

            'add_user'   => $add_user,

            'uniacid'    => $uniacid,

            'user_id'    => $user_id,

            'class_id'   => $input['class_id'],

            'store_id'   => $data['store_id'],

            'coach_id'   => $input['coach_id'],

            'class_date_id'=> 0,

            'cate_id'    => $data['cate_id'],

            'room_id'    => 0,

            'time_long'  => $data['time_long'],

            'start_time' => $input['start_time'],

            'end_time'   => $input['end_time'],

            'title'      => $data['title'],

            'difficulty' => $data['difficulty'],

            'card_id'    => $input['card_id'],

            'card_type'  => $check_card['card_type'],

            'num'        => $input['num'],

            'value'      => $check_card['value'],

            'true_value' => $check_card['true_value'],

            'record_text'=> $input['record_text'],
            //课程类型
            'class_type' => 1,

            'status'     => 1,
        ];

        Db::startTrans();

        $res = $record_model->dataAdd($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }
        //添加流水记录
        $water_model = new CardWater();

        $record_id = $this->getLastInsID();

        $res = $water_model->recordWaterAdd($record_id,$insert['true_value'],$insert['card_id']);

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }
        //减会员卡对余额或者次数
        if(in_array($check_card['card_type'],[1,2])){

            $res = $card_model->dataUpdate(['id'=>$input['card_id']],['true_value'=>$check_card['card_true_value']-$insert['true_value']]);
        }

        if($res==0){

            Db::rollback();

            $this->errorMsg('预约失败');
        }

        //发送雷达消息
        $modelCount = new RadarCount();

//        $input['staff_id'] = !empty($input['staff_id'])?$input['staff_id']:0;
        //员工暂不通知
        $arr = [$input['coach_id']];

        $arr = array_unique($arr);

        foreach ($arr as $value){

            if($value){

                $data = array(

                    'user_id' => $user_id,

                    'to_uid'  => $value,

                    'type'    => 671,

                    'sign'    => 'view',

                    'scene'   => 0,

                    'uniacid' => $uniacid,

                    'target'  => $record_id,

                    'start_time' => $insert['start_time'],

                    'create_time' => time()
                );
                //时长
                $result = $modelCount->insert( $data );

                if($result==0){

                    Db::rollback();

                    $this->errorMsg('预约失败');
                }

                $insert_id = $modelCount->getLastInsID();

                longbingSendWxServiceNotice($insert_id);

            }

        }
        Db::commit();
        //订阅消息预约成功通知
        $this->successOrderTmp($insert);

        return true;

    }






    /**
     * @author chenniang
     * @DataTime: 2019-12-27 19:19
     * @功能说明:发送订阅消息（取消预约通知）
     */
    public function cancelOrderTmp($order){

        $user_model = new User();
        //获取用户的open_id
        $openid = $user_model->where(['id'=>$order['user_id']])->value('openid');

        //访问页面
        $page = '/appclass/pages/order/list';
        //模版消息model
        $tmpl_model = new TmplConfig();
        //获取模版
        $tmpl       = $tmpl_model->where(['uniacid'=>$order['uniacid'],'tmpl_name'=>'cancel_appclass_order'])->find();
        //如果未添加模版消息 则不发送
        if(empty($tmpl)){

            return true;
        }else{
            $tmpl = $tmpl->toArray();
        }
        //模版id
        $tmpl_id = $tmpl['tmpl_id'];
//        //模版类容
//        $key_worlds = $tmpl['kidList'];
        $service_model = new WxTmpl($order['uniacid']);
        //模版的key
        $key_worlds    = $service_model::getTmplKey($tmpl_id);

        if(!empty($openid)&&!empty($tmpl_id)&&!empty($key_worlds)){

            //验证模版内容
            if(!is_array($key_worlds)){

                return true;
            }
            //发送内容
            $send_data = array(

                $key_worlds[1]=>array(
                    //预约项目
                    'value'=>$order['title'],
                ),
                $key_worlds[2]=>array(
                    //开始时间
                    'value'=>date('Y-m-d H:i',$order['start_time']),
                )
            );
            //模版消息库类
            $tmpl_sever = new WxTmpl($order['uniacid']);
            //发送模版消息
            $res = $tmpl_sever::sendTmpl($openid,$tmpl_id,$send_data,$page);

        }
        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2019-12-27 19:19
     * @功能说明:发送订阅消息（预约成功通知）
     */
    public function successOrderTmp($order){

        $user_model = new User();
        //获取用户的open_id
        $openid = $user_model->where(['id'=>$order['user_id']])->value('openid');
        //访问页面
        $page = '/appclass/pages/order/list';
        //模版消息model
        $tmpl_model = new TmplConfig();
        //获取模版
        $tmpl       = $tmpl_model->where(['uniacid'=>$order['uniacid'],'tmpl_name'=>'pay_appclass_order'])->find();

        //如果未添加模版消息 则不发送
        if(empty($tmpl)){

            return true;
        }else{
            $tmpl = $tmpl->toArray();
        }
        //模版id
        $tmpl_id = $tmpl['tmpl_id'];
//        //模版类容
//        $key_worlds = $tmpl['kidList'];
        $service_model = new WxTmpl($order['uniacid']);
        //模版的key
        $key_worlds    = $service_model::getTmplKey($tmpl_id);

        if(!empty($openid)&&!empty($tmpl_id)&&!empty($key_worlds)){
            //验证模版内容
            if(!is_array($key_worlds)){
                return true;
            }
            //发送内容
            $send_data = array(

                $key_worlds[1]=>array(
                    //预约项目
                    'value'=>$order['title'],
                ),
                $key_worlds[2]=>array(
                    //开始时间
                    'value'=>date('Y-m-d H:i',$order['start_time']),
                )
            );
            //模版消息库类
            $tmpl_sever = new WxTmpl($order['uniacid']);
            //发送模版消息
            $res = $tmpl_sever::sendTmpl($openid,$tmpl_id,$send_data,$page);
//             dump($send_data,$res,$tmpl_id);exit;

        }
        return true;
    }

}